# -*- coding: utf-8 -*-
from flask import request
from flask_restx import Resource, Namespace, fields

from utils.response import ResponseMaker
from app.models.order import Order
from app.extentions import db
from utils.jwt import JWTUtil


order_api = Namespace("order", description="订单", path="/api/order")

order_list_model = order_api.model('OrderListModel', {
    'page_number': fields.Integer(required=True, description='页码', default=1),
    'page_size': fields.Integer(required=True, description='密码', default=10)
})


@order_api.route('/list')
class CartListAPI(Resource):

    @order_api.doc(body=order_list_model, description='历史订单列表')
    @JWTUtil.verify_token_decorator(request)
    def post(self, *args, **kwargs):
        data = request.get_json(force=True)
        page_number = data.get("page_number")
        page_size = data.get("page_size")

        total = Order.query.filter(Order.uid==request.user.id).count()
        order_obj_list = Order.query.filter(Order.uid==request.user.id).paginate(page=page_number, per_page=page_size, error_out=False).items
        order_list = [order.serialize() for order in order_obj_list]

        resp_data = {
            "total": total,
            "data": order_list
        }

        return ResponseMaker.success(data=resp_data)